package com.hnkywl.sts.action.cwgl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.cggl.CgdDao;
import com.hnkywl.sts.dao.cwgl.CgrkDao;
import com.hnkywl.sts.dao.cwgl.VoucherType;
import com.hnkywl.sts.dao.xsgl.XsdDao;
import com.hnkywl.sts.entity.cggl.Cgd;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.entity.xsgl.Xsd;
import com.hnkywl.sts.service.cwgl.CgrkService;
import com.hnkywl.sts.service.cwgl.VoucherService;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.UUIDGenerator;
import com.hnkywl.sts.util.XmlParse;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-15
 * Time: 下午2:17
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/cwgl/cgrk")
public class CgrkAction extends SimpleActionSupport {
    private CgrkService cgrkService;
    private CgrkDao cgrkDao;

    //查询界面字段
    private String wzbm;
    private String cgdh;
    private Long dzckId;
    private Date fhrqStart;
    private Date fhrqEnd;
    private String cglx;
    private Long cggsId;
    private Long cgbmId;
    private Long cgrId;
    private String zt;
    private String sftzd;
    private String htType;
    private Long gysid;
    private String rkzt;
    private String zdr;
    private Boolean dlkhcgd=true;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    private Long cgdwId;
    private Date CurrentTime = new Date();

    private GsjgService  gsjgService;

   //导入界面字段
    private Yg yg;
    private String gsmc;
    private String systemDate;
    private Long cgrkId;
    private YgService ygService;
    private Map<String, String> result = new HashMap<String, String>();
    private Integer attachment;

    private Map<String, Object> voucherMaster = new HashMap<String, Object>();

    //导入部分字段
    private VoucherService voucherService;
    private Map<String, Object> voucher;
    private XmlParse xmlParse;
    private Date cgrkSystemDate;
    private Gson gson = new Gson();



    @Action("cgrkList_aaa")
    public void cgrkList_aaa(){
        try {
            if (cgrkId != null) {
                Gsjg g = this.gsjgService.findGsjgDetail(cgdwId);
                if (g instanceof Nbgs) {
                    cggsId = cgdwId;
                } else {
                    cgbmId = cgdwId;
                }
                this.renderJson(this.cgrkService.findAllCgrkList(wzbm, cgdh, dzckId, fhrqStart, fhrqEnd, cglx, cggsId, cgbmId, cgrId, zt, sftzd, htType, gysid, "true", zdr, dlkhcgd, page, rows));
            } else {
                this.renderJson(this.cgrkService.findAllCgrkList(wzbm, cgdh, dzckId, fhrqStart, fhrqEnd, cglx, cggsId, cgbmId, cgrId, zt, sftzd, htType, gysid, "true", zdr, dlkhcgd, page, rows));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //贴现承兑进入票据页面
    @Action("cgrk_insert")
    public String cgrkInsert(){
        yg = ygService.findYgDetail(YgService.getCurrentYg().getId());
        gsmc = cgrkDao.get(cgrkId).getSbgs().getJc();
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        systemDate =fmt.format(new Date()) ;
        voucherMaster = this.cgrkService.getVoucherMaster(cgrkId, attachment, null, null, null, yg);
        return SUCCESS;
    }

    @Action("findcgrkfl")
    public void findcgrkFl(){
        renderText(cgrkService.findQtpzfl(cgrkId));
    }

    //财务收付导入
    @Action("generate_voucher")
    public void generateVoucher() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy");
        SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("MM");
        try {
            if (StringUtils.isEmpty(YgService.getCurrentYgJiuqiUsername())) {
                throw new RuntimeException("您没有U8用户名，不具有导入权限");
            }
            if (StringUtils.isEmpty(YgService.getCurrentYgJiuqiPassword())) {
                throw new RuntimeException("您没有U8密码，不具有导入权限");
            }
            String uuid = UUIDGenerator.getUUID();
            Integer year = GregorianCalendar.getInstance().get(Calendar.YEAR);
            Integer mouth = GregorianCalendar.getInstance().get(Calendar.MONTH);
            yg = ygService.findYgDetail(YgService.getCurrentYg().getId());
            Boolean begin = voucherService.beginGenerateVoucher(cgrkId, VoucherType.CGRK);
            if (!begin)
                throw new RuntimeException("其他用户已经开始导入该凭证，请选择其他凭证进行导入!");
            //凭证分录
            voucher = this.cgrkService.getU8VoucherItems(cgrkId, uuid, null,cgrkSystemDate);
            //凭证头
            Map<String, Object> msgheader = voucherService.getMsgHeader();
            voucher.put("msgheader", msgheader);
            //凭证主表信息
            Map<String, Object> voucherMaster =
                    this.cgrkService.getVoucherMaster(cgrkId,null, uuid,null,null,yg);
            //将头部可以修改的部分的修改值重新赋值
            voucherMaster.put("createdate",simpleDateFormat.format(cgrkSystemDate));
            voucherMaster.put("acctyear",simpleDateFormat2.format(cgrkSystemDate));
            voucherMaster.put("acctperiod",simpleDateFormat3.format(cgrkSystemDate));
            voucherMaster.put("attachment",attachment);
            voucherMaster.put("memo", VoucherType.CGRK.getTag()+cgrkId);//备注，在凭证删除时使用
            this.cgrkService.cgrkChangeDrrq(cgrkId, cgrkSystemDate);
            voucher.put("master", voucherMaster);
            String r = voucherService.generateVoucher(voucher);
            result = xmlParse.getContent(r);
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                String vchrid = result.get(XmlParse.VCHRID);
                String vchrnum = result.get(XmlParse.VCHRNUM);
                Boolean success = voucherService.generateVoucherSuccess(uuid, vchrid, vchrnum, year, cgrkId, VoucherType.CGRK);
                if (!success)
                    throw new RuntimeException("凭证导入成功，但凭证信息回写失败！");
            }
        } catch (Exception e) {
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        renderJson(gson.toJson(result));
    }

    /**
     * 撤销凭证
     */
    @Action("revoke_voucher")
    public void revokeVoucher() {
        try {
            voucherService.checkJiuqiUsernamePassword();
            Boolean begin = voucherService.beginRevokeVoucher(cgrkId, VoucherType.CGRK);
            if (!begin)
                throw new RuntimeException("其他用户已经开始撤销该凭证，请选择其他凭证进行撤销!");
            Map<String, Object> msgheader = voucherService.getMsgHeader();
            Map<String, Object> revokeInfo = this.cgrkService.getRevokeInfo(cgrkId);
            Map<String, Object> info = new HashMap<String, Object>();
            info.put("msgheader", msgheader);
            info.put("revokeinfo", revokeInfo);
            revokeInfo.put("memo", VoucherType.CGRK.getTag()+cgrkId);//备注，在凭证删除时使用
            String r = voucherService.revokeVoucher(info);
            result = xmlParse.getContent(r);
            //结果回写
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                Boolean success = voucherService.revokeVoucherSuccess(cgrkId, VoucherType.CGRK);
                if (!success)
                    throw new RuntimeException("凭证撤销成功，但凭证状态回写失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        renderJson(gson.toJson(result));
    }

    /**
     * 重置凭证
     */
    @Action("rest_voucher")
    public void restVoucher() {
        renderJson(voucherService.restVoucher(cgrkId, VoucherType.CGRK));
    }





    public String getWzbm() {
        return wzbm;
    }

    public void setWzbm(String wzbm) {
        this.wzbm = wzbm;
    }

    public String getCgdh() {
        return cgdh;
    }

    public void setCgdh(String cgdh) {
        this.cgdh = cgdh;
    }

    public Long getDzckId() {
        return dzckId;
    }

    public void setDzckId(Long dzckId) {
        this.dzckId = dzckId;
    }

    public Date getFhrqStart() {
        return fhrqStart;
    }

    public void setFhrqStart(Date fhrqStart) {
        this.fhrqStart = fhrqStart;
    }

    public Date getFhrqEnd() {
        return fhrqEnd;
    }

    public void setFhrqEnd(Date fhrqEnd) {
        this.fhrqEnd = fhrqEnd;
    }

    public String getCglx() {
        return cglx;
    }

    public void setCglx(String cglx) {
        this.cglx = cglx;
    }

    public Long getCggsId() {
        return cggsId;
    }

    public void setCggsId(Long cggsId) {
        this.cggsId = cggsId;
    }

    public Long getCgbmId() {
        return cgbmId;
    }

    public void setCgbmId(Long cgbmId) {
        this.cgbmId = cgbmId;
    }

    public Long getCgrId() {
        return cgrId;
    }

    public void setCgrId(Long cgrId) {
        this.cgrId = cgrId;
    }

    public String getZt() {
        return zt;
    }

    public void setZt(String zt) {
        this.zt = zt;
    }

    public String getSftzd() {
        return sftzd;
    }

    public void setSftzd(String sftzd) {
        this.sftzd = sftzd;
    }

    public String getHtType() {
        return htType;
    }

    public void setHtType(String htType) {
        this.htType = htType;
    }

    public Long getGysid() {
        return gysid;
    }

    public void setGysid(Long gysid) {
        this.gysid = gysid;
    }

    public String getRkzt() {
        return rkzt;
    }

    public void setRkzt(String rkzt) {
        this.rkzt = rkzt;
    }

    public String getZdr() {
        return zdr;
    }

    public void setZdr(String zdr) {
        this.zdr = zdr;
    }

    public Boolean getDlkhcgd() {
        return dlkhcgd;
    }

    public void setDlkhcgd(Boolean dlkhcgd) {
        this.dlkhcgd = dlkhcgd;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Long getCgdwId() {
        return cgdwId;
    }

    public void setCgdwId(Long cgdwId) {
        this.cgdwId = cgdwId;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    public String getGsmc() {
        return gsmc;
    }

    public void setGsmc(String gsmc) {
        this.gsmc = gsmc;
    }

    public String getSystemDate() {
        return systemDate;
    }

    public void setSystemDate(String systemDate) {
        this.systemDate = systemDate;
    }



    public Long getCgrkId() {
        return cgrkId;
    }

    public void setCgrkId(Long cgrkId) {
        this.cgrkId = cgrkId;
    }


    public Map<String, String> getResult() {
        return result;
    }

    public void setResult(Map<String, String> result) {
        this.result = result;
    }

    public Integer getAttachment() {
        return attachment;
    }

    public void setAttachment(Integer attachment) {
        this.attachment = attachment;
    }

    public Map<String, Object> getVoucherMaster() {
        return voucherMaster;
    }

    public void setVoucherMaster(Map<String, Object> voucherMaster) {
        this.voucherMaster = voucherMaster;
    }



    public Map<String, Object> getVoucher() {
        return voucher;
    }

    public void setVoucher(Map<String, Object> voucher) {
        this.voucher = voucher;
    }

    public XmlParse getXmlParse() {
        return xmlParse;
    }

    public void setXmlParse(XmlParse xmlParse) {
        this.xmlParse = xmlParse;
    }

    public Date getCgrkSystemDate() {
        return cgrkSystemDate;
    }

    public void setCgrkSystemDate(Date cgrkSystemDate) {
        this.cgrkSystemDate = cgrkSystemDate;
    }

    public Gson getGson() {
        return gson;
    }

    public void setGson(Gson gson) {
        this.gson = gson;
    }

    public Date getCurrentTime() {
        return CurrentTime;
    }

    public void setCurrentTime(Date currentTime) {
        CurrentTime = currentTime;
    }

    @Autowired
    public void setCgrkService(CgrkService cgrkService) {
        this.cgrkService = cgrkService;
    }

    @Autowired
    public void setVoucherService(VoucherService voucherService) {
        this.voucherService = voucherService;
    }
    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }
    @Autowired
    public void setCgrkDao(CgrkDao cgrkDao) {
        this.cgrkDao = cgrkDao;
    }
    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }

}
